clear
set more off

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    gen female = sex==2
    gen test = 1 if baseline_sample==1
    bysort data: egen survey_data= max(test)
    tab data if survey_data==1
    
    file open Table_SumStats using "$Mydirectory2/main_figures_tables/table2.tex", write replace

    file write Table_SumStats "\begin{tabularx}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill} l c c c c c c c}" _n
    file write Table_SumStats "\toprule" _n
    file write Table_SumStats "\addlinespace[2ex]" _n   
    file write Table_SumStats "& 1910s & 1920s & 1930s & 1940s & 1950s & 1960s & 1970s  \\" _n

    file write Table_SumStats "\addlinespace[1ex]" _n
    file write Table_SumStats "\midrule" _n
    
* 1. Fathers demographics
    file write Table_SumStats "\addlinespace[2ex]" _n
    file write Table_SumStats "\textit{Father demographics:} & & & & & & & \\" _n
    file write Table_SumStats "\addlinespace[1.5ex]" _n
    
    foreach var of varlist fatherforeign dad_hs_ed dad_coll_ed fatherfarm {
        if "`var'"=="fatherfarm" local label1 "Farming occupation"
        if "`var'"=="fatherforeign" local label1 "Foreign-born"
        if "`var'"=="dad_hs_ed" local label1 "High school educated"
        if "`var'"=="dad_coll_ed" local label1 "College educated"

            file write Table_SumStats "\quad `label1' "

            levelsof decade, local(decades)
            foreach x of local decades {
            
            sum `var' if decade==`x' & baseline_sample==1 
                local number =`r(mean)'
                local number2: display %-09.2fc `number'    
                file write Table_SumStats "& `number2' "
            }
    
        file write Table_SumStats " \\" _n
    }
    
* 2. Respondent demographics  
    file write Table_SumStats "\addlinespace[2ex]" _n
    file write Table_SumStats "\textit{Respondent demographics:} & & & & & & & \\" _n
    file write Table_SumStats "\addlinespace[1.5ex]" _n
    
    foreach var of varlist female age black hs_ed coll_ed moved_region unionR veteran  {
    
        if "`var'"=="female" local label1 "Female"
        if "`var'"=="age" local label1 "Age"
        if "`var'"=="black" local label1 "Black"
        if "`var'"=="hs_ed" local label1 "High school educated"
        if "`var'"=="coll_ed" local label1 "College educated"
        if "`var'"=="moved_region" local label1 "Moved regions"
        if "`var'"=="unionR" local label1 "Union member (men)"
        if "`var'"=="veteran" local label1 "Veteran (men)"
    
        file write Table_SumStats "\quad `label1' "

        
        if "`var'"=="unionR" | "`var'"=="veteran" {
            local cond_sex "& sex==1"
        }
        else {
            local cond_sex " "
        }

        levelsof decade, local(decades)
        foreach x of local decades {
            
            if `x'==1910 & "`var'"=="veteran" file write Table_SumStats "& \textemdash " //note: no veterans in baseline sample for this decade
            
            else {
                sum `var' if decade==`x' & baseline_sample==1 `cond_sex' 
                    local number =`r(mean)'
                    local number2: display %-09.2fc `number'    
                    file write Table_SumStats "& `number2' "
            }
            
        }
        
        file write Table_SumStats " \\" _n
    }
    
* 3. Father income
    file write Table_SumStats "\addlinespace[2ex]" _n
    file write Table_SumStats "\textit{Parental income:} & & & & & & & \\" _n
    file write Table_SumStats "\addlinespace[1.5ex]" _n
    
        //Important because father_income_baseline is in logs
        replace father_income_baseline = exp(father_income_baseline)
    
    foreach var of varlist father_income_baseline father_inc_missing rank_father_baseline {

        if "`var'"=="father_income_baseline" local label1 "Predicted income (1950\textdollar)"
        if "`var'"=="father_inc_missing" local label1 "Missing income"
        if "`var'"=="rank_father_baseline" local label1 "Rank"

        file write Table_SumStats "\quad `label1' "

        if "`var'"!="father_inc_missing" local cond1 "if baseline_sample==1"
        else local cond1 "if survey_data==1 & race<." 

        levelsof decade, local(decades)
        foreach x of local decades {
        
            if "`var'"!="father_inc_missing" {
                sum `var' `cond1' & decade==`x' 
            }
            if "`var'"=="father_inc_missing" {
                sum `var' `cond1' & decade==`x' 
            }
                local number =`r(mean)'
                
                if "`var'"=="father_income_baseline" {
                    local number2: display %-09.0fc `number'    
                    file write Table_SumStats "& `number2' "
                }
                else {
                    local number2: display %-09.2fc `number'    
                    file write Table_SumStats "& `number2' "
                }
        }
            
        file write Table_SumStats " \\" _n
    }

* 4. Respondent income  
    file write Table_SumStats "\addlinespace[2ex]" _n
    file write Table_SumStats "\textit{Respondent income:} & & & & & & & \\" _n
    file write Table_SumStats "\addlinespace[1.5ex]" _n
    
    foreach var of varlist fam_inc_real fam_inc_missing bottomcoded_son topcoded_son rank_son_baseline  {
    
        if "`var'"=="fam_inc_real" local label1 "Family income (1950\textdollar)"
        if "`var'"=="fam_inc_missing" local label1 "Missing income"
        if "`var'"=="bottomcoded_son" local label1 "Bottom coded"
        if "`var'"=="topcoded_son" local label1 "Top coded"
        if "`var'"=="rank_son_baseline" local label1 "Family income rank"

        if "`var'"!="fam_inc_missing" local cond1 "if baseline_sample==1"
        else local cond1 "if survey_data==1 & race<."

        file write Table_SumStats "\quad `label1' "

        levelsof decade, local(decades)
        foreach x of local decades {
        
            if "`var'"!="fam_inc_missing" {
                sum `var' `cond1' & decade==`x' 
            }
            if "`var'"!=="fam_inc_missing" {
                sum `var' `cond1' & decade==`x' 
            }   
            
            local number =`r(mean)'
            
            if "`var'"=="fam_inc_real" {
                local number2: display %-09.0fc `number'    
                file write Table_SumStats "& `number2' "
            }
            else {
                local number2: display %-09.2fc `number'    
                file write Table_SumStats "& `number2' "
            }
        }
        
        file write Table_SumStats " \\" _n
    }
    
* 5. Number of observations
    file write Table_SumStats "\addlinespace[2ex]" _n
    file write Table_SumStats "\midrule" _n 
    file write Table_SumStats "\addlinespace[1ex]" _n
    
    file write Table_SumStats "Observations"
    
    levelsof decade, local(decades)
    foreach x of local decades {
        count if decade==`x' & baseline_sample==1  
        local number =`r(N)'
        local number2: display %-09.0fc `number'    
        file write Table_SumStats "& `number2' "
    }
    
    file write Table_SumStats " \\" _n
    
    file write Table_SumStats "\addlinespace[1ex]" _n
    file write Table_SumStats "\bottomrule" _n
    file write Table_SumStats "\end{tabularx}" _n
    file close Table_SumStats
